﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;

namespace Controllers
{
    [Authentication]
    public class BaseController : Controller
    {
        //该变量不能声明为Static,因为:ctx.LoadOptions必须在实例化前指定
        protected ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);

        protected IList<int> getAuthedDeptIds(string controllerName,string idStr,out int empId)
        {
            IList<int> deptIdList = null;
            empId = 0;
            var d = new DeptController();
            if (!string.IsNullOrWhiteSpace(idStr))
            {
                deptIdList = d.GetChildDeptIdList(int.Parse(idStr));
            }
            else
            {
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                IList<EmpRole> empRole = ebi.EmpRoles;
                List<int> empRoleIds = new List<int>();
                foreach (EmpRole er in empRole)
                {
                    empRoleIds.Add(er.RoleId);
                }
                List<int> sessionDeptIdList = Session["deptIdList"] as List<int>;
                deptIdList = d.GetDeptIdListByController(controllerName, empRoleIds, sessionDeptIdList);
                if (deptIdList.Count == 0)
                {
                    empId = ebi.Id;
                }
            }
            return deptIdList;
        }
    }
}